package com.bite.extensions.controller;

import com.bite.extensions.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;
    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    @Resource(name = "transRabbitTemplate1")
    private RabbitTemplate transRabbitTemplate1;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE, "ack", "consumer ack mode test...");
        return "消息发送成功!";
    }

    @RequestMapping("/pres")
    public String pres() {
        Message message = new Message("Presistent test...".getBytes(), new MessageProperties());
        //消息持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        System.out.println(message);
        rabbitTemplate.convertAndSend(Constants.PRES_EXCHANGE, "pres", message);
        return "消息发送成功";
    }

    @RequestMapping("/confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE, "confirm", "confirm test...", correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/returns")
    public String returns() {
        CorrelationData correlationData = new CorrelationData("3");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE, "confirm111", "returns test...", correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, "retry", "retry test...");
        return "消息发送成功";
    }

    @RequestMapping("/ttl")
    public String ttl() {
        System.out.println("ttl!!!");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test... 30s", message -> {
            message.getMessageProperties().setExpiration("30000");//单位为毫秒，设置30秒后过期
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test... 10s", message -> {
            message.getMessageProperties().setExpiration("10000");//单位为毫秒，设置10秒后过期
            return message;
        });
        return "消息发送成功";
    }

    @RequestMapping("/ttl2")
    public String ttl2() {
        System.out.println("ttl2!!!");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl2 test 20s...");
        return "消息发送成功";
    }

    @RequestMapping("/dl")
    public String dl() {
        System.out.println("dl test !!!");
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "dl test ...");
        System.out.printf("%tc 消息发送成功\n", new Date());
        return "消息发送成功";
    }

    @RequestMapping("/delay")
    public String delay() {
        System.out.println("delay!!!");
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "delay test... 30s", message -> {
            message.getMessageProperties().setExpiration("30000");//单位为毫秒，设置30秒后过期
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "delay test... 10s", message -> {
            message.getMessageProperties().setExpiration("10000");//单位为毫秒，设置10秒后过期
            return message;
        });

        System.out.printf("%tc 消息发送成功\n", new Date());
        return "消息发送成功";
    }

    @RequestMapping("/delay2")
    public String delay2() {
        System.out.println("delay!!!");
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test... 30s", message -> {
            message.getMessageProperties().setDelayLong(30000l);//单位为毫秒，设置30秒后过期
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test... 10s", message -> {
            message.getMessageProperties().setDelayLong(10000l);//单位为毫秒，设置10秒后过期
            return message;
        });
        System.out.printf("%tc 消息发送成功\n", new Date());
        return "消息发送成功";
    }

    @Transactional
    @RequestMapping("/trans")
    public String trans() {
        System.out.println("trans test...");
        transRabbitTemplate1.convertAndSend(Constants.TRANS_EXCHANGE, "trans", "trans test 1...");
        int num = 5/0;
        transRabbitTemplate1.convertAndSend(Constants.TRANS_EXCHANGE, "trans", "trans test 2...");
        return "消息发送成功";
    }


    @RequestMapping("/qos")
    public String qos() {
        System.out.println("qos test...");
        for (int i = 0; i < 15; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE, "qos", "qos test i..."+i);
        }
        return "消息发送成功";
    }
//消息持久化
//message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
}